//
//  AppDelegate.m
//  Riji
//
//  Created by liujianlong on 2022/8/17.
//

#import "AppDelegate.h"
#import "BaseTabBarViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    bg_setDisableCloseDB(YES);
    self.user = [[RUserModel bg_findAll:nil] firstObject];
    [self updateCity];
    CGRect frame = [[UIScreen mainScreen] bounds];
    self.window = [[UIWindow alloc] initWithFrame:frame];
    self.window.rootViewController = [[BaseTabBarViewController alloc] init];
    [self.window makeKeyAndVisible];
    return YES;
}

#pragma mark - 用户信息
- (void)setUser:(RUserModel *)user{
    _user = user;
    if (user == nil) {
        _user = [RUserModel new];
    }
    NSString *token = (user != nil && user.token != nil && [user.token length] > 0) ? user.token : @"";
    [CCHttp http].header = @{@"Authorization":token};
}

#pragma mark - 获取城市列表
- (void)updateCity{
    self.city = [RCityModel bg_findAll:nil];
    [CCHttp http].basicURLString = @"";
    NSString *url = [NSString stringWithFormat:@"%@?key=%@",api_city,kkey];
    [CCHttp get:url para:nil header:nil success:^(id  _Nonnull response) {
        NSArray *result = [response valueForKey:@"result"];
        if (result != nil && ![result isKindOfClass:[NSNull class]] && result.count > 0 ) {
            self.city = [RCityModel mj_objectArrayWithKeyValuesArray:result];
            [RCityModel bg_saveOrUpdateArray:self.city];
        }
    } fail:^(NSError * _Nonnull error) { }];
    [CCHttp http].basicURLString = baseurl;
}
#pragma mark - 定位
-(RLocation *)location{
    if (!_location) {
        _location = [RLocation new];
    }
    return _location;
}

@end
